import * as api from '../../service/api'

const state = {
	URL : {
		shop : 'https://shop.hidlq.com/#/',
		pc : 'https://www.hidlq.com/#/',
		upload : '/v1/pn/upload',
	},
	common : {
		imgMaxSize : 2, //最大2M
	},
  	adList : [],
  	typeList : [],
  	columnList : [],
  	configInfo : {},
  	distributionSystemList : [],
  	distributionLevelList : [],
}

const getters = {
	adList : state => state.adList,
	typeList : state => state.typeList,
	columnList : state => state.columnList,
	configInfo : state => state.configInfo,
	distributionSystemList : state => state.distributionSystemList,
	distributionLevelList : state => state.distributionLevelList,
	URL : state => state.URL,
	common : state => state.common,
}

const actions = {
	async getAdList ({ commit }, para) {
		let res = await api.adList(1, 20);
		console.log(res);
		if(res.errcode === 0){
			commit('setAdList', res.data.results);
		}
	},

	async updateAd ({ commit }, para) {
		let res = await api.updateAd(
			para.id,
			para.url,
			'0',
			para.asort,
			para.del_flg
		);
		console.log(res);
		if(res.errcode === 0){
			if(para.cb){
				para.cb(res.data);
			}
		}else{
			if(para.err){
				para.err(res.errmsg);
			}
		}
	},

	async getTypeList ({ commit }, para) {
		let res = await api.typeList();
		console.log(res);
		if(res.errcode === 0){
			commit('setTypeList', res.data);
		}
	},

	async updateType ({ commit }, para) {
		let res = await api.updateType(
			para.id,
			para.logo,
			para.type_name,
			para.asort,
			para.del_flg
		);
		console.log(res);
		if(res.errcode === 0){
			if(para.cb){
				para.cb(res.data);
			}
		}else{
			if(para.err){
				para.err(res.errmsg);
			}
		}
	},

	async getColumnList ({ commit }, para) {
		let res = await api.columnList();
		console.log('getColumnList');
		console.log(res);
		if(res.errcode === 0){
			commit('setColumnList', res.data);
		}
	},


	async updateColumn ({ commit }, para) {
		let res = await api.updateColumn(
			para.id,
			para.column_name,
			para.mui,
			para.state,
			para.asort,
			para.del_flg
		);
		console.log(res);
		if(res.errcode === 0){
			if(para.cb){
				para.cb(res.data);
			}
		}else{
			if(para.err){
				para.err(res.errmsg);
			}
		}
	},


	async getConfigInfo ({ commit }, para) {
		let res = await api.configInfo();
		console.log(res);
		if(res.errcode === 0){
			commit('setConfigInfo', res.data);
			para.cb(res.data);
		}
	},


	async updateConfig_ad ({ commit }, para) {
		let res = await api.updateConfig_ad(
			para.id,
			para.ad,
			para.topimgsize
		);
		console.log(res);
		if(res.errcode === 0){
			if(para.cb){
				para.cb(res.data);
			}
		}else{
			if(para.err){
				para.err(res.errmsg);
			}
		}
	},


	async updateConfig_describe ({ commit }, para) {
		let res = await api.updateConfig_describe(
			para.id,
			para.isshow,
			para.describe
		);
		console.log(res);
		if(res.errcode === 0){
			if(para.cb){
				para.cb(res.data);
			}
		}else{
			if(para.err){
				para.err(res.errmsg);
			}
		}
	},

	async updateConfig_type ({ commit }, para) {
		let res = await api.updateConfig_type(
			para.id,
			para.type,
			para.typeimgsize
		);
		console.log(res);
		if(res.errcode === 0){
			if(para.cb){
				para.cb(res.data);
			}
		}else{
			if(para.err){
				para.err(res.errmsg);
			}
		}
	},

	async updateConfig_share ({ commit }, para) {
		let res = await api.updateConfig_share(
			para.id,
			para.qrcodeimg,
			para.share_title,
			para.share_describe,
			para.share_img,
			para.level_code,
			para.mobile,
		);
		console.log(res);
		if(res.errcode === 0){
			if(para.cb){
				para.cb(res.data);
			}
		}else{
			if(para.err){
				para.err(res.errmsg);
			}
		}
	},


	async getDistributionSystemList ({ commit }, para) {
		let res = await api.distributionSystemList();
		console.log(res);
		if(res.errcode === 0){
			commit('setDistributionSystemList', res.data);
		}
	},

	async updateDistributionSystem ({ commit }, para) {
		let res = await api.updateDistributionSystem(
			para.code,
			para.title,
			para.isadopt,
			para.describe
		);
		console.log(res);
		if(res.errcode === 0){
			if(para.cb){
				para.cb(res.data);
			}
		}else{
			if(para.err){
				para.err(res.errmsg);
			}
		}
	},



	async getDistributionLevelList ({ commit }, para) {
		let res = await api.distributionLevelList();
		console.log(res);
		if(res.errcode === 0){
			res.data.results.unshift({ title : '全部级别', code: '' });
			commit('setDistributionLevelList', res.data);
		}
	},


	async updateDistributionLevel ({ commit }, para) {
		let res = await api.updateDistributionLevel(
			para.id,
			para.code,
			para.title
		);
		console.log(res);
		if(res.errcode === 0){
			if(para.cb){
				para.cb(res.data);
			}
		}else{
			if(para.err){
				para.err(res.errmsg);
			}
		}
	},


	async releaseProduct ({ commit }, para) {
		let res = await api.releaseProduct(
			para.id
		);
		console.log(res);
		if(res.errcode === 0){
			if(para.cb){
				para.cb(res.data);
			}
		}else{
			if(para.err){
				para.err(res.errmsg);
			}
		}
	},


}

const mutations = {
	setAdList (state, obj) {
        state.adList = obj;
    },
    setTypeList (state, obj) {
        state.typeList = obj;
    },
    setColumnList (state, obj) {
        state.columnList = obj;
    },
    setConfigInfo (state, obj) {
        state.configInfo = obj;
    },
    setDistributionSystemList (state, obj) {
        state.distributionSystemList = obj;
    },
    setDistributionLevelList (state, obj) {
        state.distributionLevelList = obj;
    },
}

export default {
  state,
  getters,
  actions,
  mutations
}
